<?php
class CmsAction extends BaseAction{
	var $dao,$userLevel;
	var $isDel = false; //是否启用回收站
	function _initialize(){
		parent::_initialize();
		
		$this->_initChannelId();
		$this->_isAllow();
		$this->userLevel = $this->_isAllow();
		
		$this->dao = D('Cms','portal');
	}
	
	public function index(){
		$this->_getEdit();
		
		$map = $this->_getMap();
		$order = $this->_getOrder();
		$list = $this->dao->where($map)->order($order)->findPage();

		$this->assign("count", $list['count']);
		$this->assign("page",  $list['html']);
		$this->assign("list",  $list['data']);

		//获取模板
		unset($map);
		$map['id']     = $this->channelId;//dump($map);exit;
		$temp = D('Channel')->where($map)->getField('temp');
		
		$tpl=	SITE_PATH."/data/portal/tpl/".$temp.'/articlelist.html';
		$this->display($tpl);
	}
	
	function admin(){		
		$this->admin_tree();
		$this->_initCategory();
		$this->_myChannel();
		$this->getCategory($this->userLevel);
		
		$map = $this->_getMap();
		$order = $this->_getOrder();		
		$list = $this->dao->where($map)->order($order)->findPage();

		$this->assign("count", $list['count']);
		$this->assign("page",  $list['html']);
		$this->assign("cms_list",  $list['data']);
		
		$channel = D('Channel', 'portal')->findall();
		$this->assign('channel', $channel);	
		
		$this->display();		
	}
	
	function add(){
		$this->admin_tree();
		$this->_initCategory();
		$this->_myChannel();
		$this->getCategory($this->userLevel);
		
		$this->display();
	}
	
	function edit(){
		$this->admin_tree();
		$this->_initCategory();
		$this->_myChannel();
		$this->getCategory($this->userLevel);		
		
		$res = $this->_getEdit();
		if($res==false){
			$this->error('文章ID出错！');
		}
		
		$this->display();
	}
	
	function detail(){
		$id = $_REQUEST['id'];
		if(empty($id)){
			$this->error('参数出错！');
		}
		
		$cms = $this->dao->where("id='$id'")->find();
		$this->assign('cms', $cms);		
		
		$map['channelId']     = $cms['channelId'];
		$map['category'] = $cms['category'];
		$map['id']       = array('exp', '!='.$id);
		
		$list = $this->dao->where($map)->field('id,title')->order('id desc')->limit(40)->findall();
		$this->assign('list', $list);
		
		//更新点击数
		$this->dao->where("id='$id'")->setInc('hits');
		
		//获取模板
		unset($map);
		$map['id']     = $cms['channelId'];
		$temp = D('Channel')->where($map)->getField('temp');
		
		$tpl=	SITE_PATH."/data/portal/tpl/".$temp.'/article.html';
		$this->display($tpl);	
	}
   
	function _getEdit() {
		$id = $_REQUEST['cmsId'];
		if(empty($id)){
			return false;
		}
		
		$edit	=	$this->dao->where('id='.$id)->find();		
		$this->assign('edit', $edit);
		
		return true;
	}
	
	function doDeal(){
		$map['content']		=	addslashes($_POST['content']);
		$map['title']		=	t($_POST['title']);	
		$map['channelId']	=	intval( $_POST['channelId'] );	
		if(empty($map['title'])||empty($map['content'])||empty($map['channelId'])){
			$this->error('标题，内容和所属频道不能为空！');
		}
		
		$map['author']		=	t($_POST['author']) ? t($_POST['author']) : '管理员';
		$map['category']	=	$_POST['category'];		
		$map['user_id']		=   $_SESSION['mid'];

		$id = intval($_POST['id']);
		if(empty($id)){
			$map['publish_time']=	time();			
			$res = $this->dao->add($map);
		}else{
			$res = $this->dao->where("id='$id'")->save($map);
		}
		
		if($res){
			U('portal/Cms/admin',array('channelId'=>$_POST['channelId'],'category'=>$_POST['category']),true);
		}else{
			$this->error('操作失败！');
		}
	}

	public function doDel(){
		//删除指定记录
		$id = $_REQUEST['id'];
		if(empty($id)) {
			$this->error('非法操作');
		}
		
		$type = $_REQUEST['type'];

		$map['id'] = array('in', $id);	
		if($type=='revert'){//还原
			$res = $this->dao->where( $map )->save(array('isDel'=>0));
		}elseif ($type=='empty'){//清空
			$res = $this->dao->where( $map )->delete();
		}elseif($this->isDel){//进回收站
			$res = $this->dao->where( $map )->save(array('isDel'=>1));
		}else{//直接删除
			$res = $this->dao->where( $map )->delete();
		}

		if($res){
			U('portal/Cms/admin',array('channelId'=>$this->channelId),true);
		}else {
			$this->error("操作失败!");
		}
	}

	function _getMap(){
		$map['isDel'] = 0;
		if(!empty($this->channelId)){
			$map['channelId'] = $this->channelId;
		}
		//ID
		if(isset($_REQUEST['id'])&&!empty($_REQUEST['id'])) {
			//$map['id'] = $_REQUEST['id'];
		}
		//标题
		if(isset($_REQUEST['title'])&&!empty($_REQUEST['title'])) {
			$map['title'] = array("like","%".$_REQUEST['title']."%");
		}
		//用户ID
		if(isset($_REQUEST['userId'])&&!empty($_REQUEST['userId'])) {
			$map['userId'] = $_REQUEST['userId'];
		}	
		//频道
		if(isset($_REQUEST['tree'])&&!empty($_REQUEST['tree'])) {
			$map['tree'] = $_REQUEST['tree'];
		}
		//分类
		if(isset($_REQUEST['category'])&&!empty($_REQUEST['category'])) {
			$map['category'] = $_REQUEST['category'];
		}			
		
		return $map;	
	}
	
	function _initCategory(){
		$map['channelId'] = $this->channelId;
		$map['pageType']  = 'list';
		//dump($map);
		$category = D('Page', 'portal')->where($map)->field('id,name')->findall();

		$this->assign('categoryList', $category);
	}
}
?>